#ifndef RGBCODE_H 
#define RGBCODE_H

// #include <EEPROM.h> // internal EEPROM
// #include <eeprom.h>
#include <string.h>
#include <ctype.h>
#include "Logging.h"

class RGBCode
{
	public:
		RGBCode(int red=0, int green=0, int blue=0):red(red),green(green),blue(blue) {};
		
		void setRGBValue(int red, int green, int blue) {
			this->setRedValue(red);
			this->setGreenValue(green);
			this->setBlueValue(blue);
		};
		// 255070200 -> 255(R) 070(G) 200(B)
		void setRGBValueChar(char *message) {
			char ledValue[3];
			int numValue;
			// Red
			memcpy(ledValue, message, sizeof(ledValue));
			numValue = this->convertMessageValue(ledValue);
			if(numValue != -1) {
				this->setRedValue(numValue);
			}
			// Green
			memcpy(ledValue, message + 3, sizeof(ledValue));
			numValue = this->convertMessageValue(ledValue);
			if(numValue != -1) {
				this->setGreenValue(numValue);
			}
			// Blue
			memcpy(ledValue, message + 6, sizeof(ledValue));
			numValue = this->convertMessageValue(ledValue);
			if(numValue != -1) {
				this->setBlueValue(numValue);
			}
		};
		
		//TODO: char* getRGBValueAsMessage();
		
		int getRedValue() {
			return this->red;
		};
		int getGreenValue() {
			return this->green;
		};
		int getBlueValue() {
			return this->blue;
		};
		
		virtual void setRedValue(int value) {
			this->red = value;
		};
		virtual void setGreenValue(int value) {
			this->green = value;
		};
		virtual void setBlueValue(int value) {
			this->blue = value;
		};
		
		void setRedValueChar(char *message) {
			char ledValue[3];
			int numValue;
			memcpy(ledValue, message, sizeof(ledValue));
			numValue = this->convertMessageValue(ledValue);
			if(numValue != -1) {
				this->setRedValue(numValue);
			}
		};
		
		void setGreenValueChar(char *message) {
			char ledValue[3];
			int numValue;
			memcpy(ledValue, message, sizeof(ledValue));
			numValue = this->convertMessageValue(ledValue);
			if(numValue != -1) {
				this->setGreenValue(numValue);
			}
		};
		
		void setBlueValueChar(char *message) {
			char ledValue[3];
			int numValue;
			memcpy(ledValue, message, sizeof(ledValue));
			numValue = this->convertMessageValue(ledValue);
			if(numValue != -1) {
				this->setBlueValue(numValue);
			}
		};

	protected:
		unsigned char red;
		unsigned char green;
		unsigned char blue;
		
		int convertMessageValue(char ledValue[3]) {
			char numChar[3];
			bool isNumber = true;
			int numValue = -1;
			// convert value from char to int
			for(int i=0; i<=2; i++) {
				if(isdigit(ledValue[i])) {
					numChar[i] = ledValue[i];
				} else {
					isNumber = false;
				}
			}
			if(isNumber) {
				// set value to pin
				numValue = (numChar[0]-48)*100;
				numValue += (numChar[1]-48)*10;
				numValue += numChar[2]-48;
				String log = "Is Number: ";
				log += numValue;
				Logging::writeDebugLog(log);
			}
			return numValue;
		}
};
#endif // RGBCODE_H